package com.llh.demo4shiro.controller;

import com.llh.demo4shiro.model.Authority;
import com.llh.demo4shiro.service.AuthorityService;
import com.llh.demo4shiro.vo.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>CreatedAt: 2020/9/17 9:56</p>
 *
 * @author llh
 */
@RestController
@RequestMapping("authority")
public class AuthorityController {
    @Autowired
    private AuthorityService authorityService;

    @PostMapping("add")
    public JsonResult add(@RequestBody Authority authority) {
        boolean existed = authorityService.existedAuthority(authority.getName());
        JsonResult result = new JsonResult();
        if (existed) {
            result.setOk(false)
                    .setMsg("权限已存在！");
            return result;
        }
        Authority add = authorityService.add(authority);
        return result.setData(add);
    }

    @GetMapping("all")
    public JsonResult all() {
        List<Authority> all = authorityService.all();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(0)
                .setData(all);
        return jsonResult;
    }

}
